<?php

class CpcategoryController extends Core_Controller_Action 
{
	private $_api;

	private $_descript;
	
	public function init() {	
		$this->_api = new Category();	
		$this->_descript = new DescriptionCategory();
	}
	public function indexAction(){
		$page = intval( $this->_request->getParam( 'page', 1 ) );
		
		$pagerLayout = $this->_api->getListWithPager( $page, $this->view->baseUrl() );
		
		$this->view->list = $pagerLayout->execute();
		$this->view->pager = $pagerLayout;
		
		$langs = new Langs();
		$this->view->langs = $langs->getList();
	}
	public function addAction(){
		$langs = new Langs();
		$ln = $this->_getParam('ln');
		if($ln!='')
			$this->view->langs = $langs->getItem($ln);
		else
			$this->view->langs = $langs->getList();
		$currency = new Currency();
		$this->view->currency = $currency->getList();
		
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		if($this->_request->isPost())
		{
			$post = $this->_request->getPost();
			if($id)
			{
				$category = $this->_api->getItemDD( $id )->getFirst();
//				Core_Debug::pr($category->toArray());
//				exit;
			}
			else
			{
				$category = $this->_api;
				$help = $this->_api->getOrder()->getFirst();
				$category->order_num = $help->order_num + 1;
			}
			
			$category->save();
			
			//zapisywanie opisów produktów
			foreach ($this->view->langs as $one)
			{
				$dese = new DescriptionCategory();
				if($id)
					$des = $dese->getItemByCategory( $id,$one['l_id'] )->getFirst();
				else
					$des = $dese;
				$des->category_c_id = $category->c_id;
				$des->langs_l_id = $one['l_id'];
				$des->title = $post['title'][$one['l_id']];
				if($post['url'][$one['l_id']]!='')
					$des->url = $post['url'][$one['l_id']];
				else
					$des->url = $this->Seourl($post['title'][$one['l_id']]);
				$des->save();
			}
			$this->_redirect('cpcategory');
		}
		if($id)
		{
			$this->view->item = $this->_api->getItemDD( $id )->getFirst()->toArray();
			$prod_lang = array();
			foreach ($this->view->langs as $one)
			{
				$prod_lang[$one['l_id']] = $this->_descript->getItemByCategory( $id,$one['l_id'] )->getFirst()->toArray();
			}
			$this->view->des = $prod_lang;
		}

	}
	public function deleteAction(){
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		if($id)
		{
			$this->_api->deleteItem($id);
			$this->_descript->deleteByCate($id);
		}
		$this->_redirect('cpcategory');
	}
	public function changeorderAction(){
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		$where = intval( $this->_request->getParam( 'where', 0 ) );
		if($id)
		{
			if($where < 2){
				$category = $this->_api->getItem( $id )->getFirst();
				$help = $category->order_num;
				$order = $this->_api->getOrderItem( $help , $where )->getFirst();
				
				$category->order_num = $order->order_num;
				$category->save();
				
				$order->order_num = $help;
				$order->save();
			}else{
				$category = $this->_api->getItem( $id )->getFirst();
				$order = $this->_api->getOrderItem( $category->order_num , $where-2 )->getData();
				foreach ($order as $inny){
					$help = $category->order_num;
					$category->order_num = $inny->order_num;
					$inny->order_num = $help;
					$inny->save();
				}
				$category->save();
				//die(1);
			}
		}
		$this->_redirect('cpcategory');
	}

	public function Seourl( $txt ) {

		$table = Array(
		//WIN
		"xb9" => "a", "xa5" => "A", "xe6" => "c", "xc6" => "C",
		"xea" => "e", "xca" => "E", "xb3" => "l", "xa3" => "L",
		"xf3" => "o", "xd3" => "O", "x9c" => "s", "x8c" => "S",
		"x9f" => "z", "xaf" => "Z", "xbf" => "z", "xac" => "Z",
		"xf1" => "n", "xd1" => "N",
		//UTF
		"xc4x85" => "a", "xc4x84" => "A", "xc4x87" => "c", "xc4x86" => "C",
		"xc4x99" => "e", "xc4x98" => "E", "xc5x82" => "l", "xc5x81" => "L",
		"xc3xb3" => "o", "xc3x93" => "O", "xc5x9b" => "s", "xc5x9a" => "S",
		"xc5xbc" => "z", "xc5xbb" => "Z", "xc5xba" => "z", "xc5xb9" => "Z",
		"xc5x84" => "n", "xc5x83" => "N",
		//ISO
		"xb1" => "a", "xa1" => "A", "xe6" => "c", "xc6" => "C",
		"xea" => "e", "xca" => "E", "xb3" => "l", "xa3" => "L",
		"xf3" => "o", "xd3" => "O", "xb6" => "s", "xa6" => "S",
		"xbc" => "z", "xac" => "Z", "xbf" => "z", "xaf" => "Z",
		"xf1" => "n", "xd1" => "N");
		
		$txt = strtr( $txt, $table );
		
		return str_replace( ' ', '-', $txt );
	}
	
}
	